//    twitpic4j is a small Java library aimed at easy TwitPic image uploads.
//
//	  Copyright (C) 2009  Harrison Lee
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


package android.fwirk.img;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.sax.Element;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Log;
import android.util.Xml;

public class YFrog {
	
	public static final String PARAM_USERNAME 		= "username";
	public static final String PARAM_PASSWORD 		= "password";
	public static final String PARAM_MEDIA			= "media";
	public static final String PARAM_MESSAGE		= "message";
	public static final String UPLOAD_URL			= "http://yfrog.com/api/upload";
	public static final String UPLOAD_AND_POST_URL	= "http://yfrog.com/api/uploadAndPost";
	
	private String mUsername;
	private String mPassword;
	private ClientHttpRequest mRequest;
	
	public YFrog(){}
	
	public YFrog(String username, String password){
		mUsername = username;
		mPassword = password;
	}
	
	private void setUserParameters() throws IOException {
		mRequest.setParameter(PARAM_USERNAME, mUsername);
		mRequest.setParameter(PARAM_PASSWORD, mPassword);
	}
	
	public String upload(File picture) throws IOException {
		mRequest = new ClientHttpRequest(new URL(UPLOAD_URL));
		setUserParameters();
		mRequest.setParameter(PARAM_MEDIA, picture);
		String url=FeedParser.parse(mRequest.post());
		Log.d("Test",url);
		return url;
	}
	
	public String upload(byte[] picture) throws IOException {
		mRequest = new ClientHttpRequest(new URL(UPLOAD_URL));
		setUserParameters();
		mRequest.setParameter(PARAM_MEDIA, picture);
		String url=FeedParser.parse(mRequest.post());
		Log.d("Test",url);
		return url;
	}
	
	public String uploadAndPost(File picture, String message) throws IOException {
		mRequest = new ClientHttpRequest(new URL(UPLOAD_AND_POST_URL));
		setUserParameters();
		if(message != null)
			mRequest.setParameter(PARAM_MESSAGE, message);
		mRequest.setParameter(PARAM_MEDIA, picture);
		String url=FeedParser.parse(mRequest.post());
		Log.d("Test",url);
		return url;
	}
	
	
	public String uploadAndPost(byte[] picture, String message) throws IOException {
		mRequest = new ClientHttpRequest(new URL(UPLOAD_AND_POST_URL));
		setUserParameters();
		if(message != null)
			mRequest.setParameter(PARAM_MESSAGE, message);
		mRequest.setParameter(PARAM_MEDIA, picture);
		String url=FeedParser.parse(mRequest.post());
		Log.d("Test",url);
		return url;
	}
	
	public String getUsername() {
		return mUsername;
	}

	public void setUsername(String username) {
		mUsername = username;
	}

	public String getPassword() {
		return mPassword;
	}

	public void setPassword(String password) {
		mPassword = password;
	}
}

class FeedParser {
	static String url;
    public static String parse(InputStream is) {
    	url="";
    	RootElement root = new RootElement("rsp");
        Element mediaUrl = root.getChild("mediaurl");
        
        mediaUrl.setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                url=body;
            }
        });
        
        try {
            Xml.parse(is, Xml.Encoding.UTF_8, 
root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return url;
    }
}